package com.ruoyi.zsyh.controller.applet.store;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.EmptyUtils;
import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.zsyh.domain.ZsyhAgentPartsattribute;
import com.ruoyi.zsyh.domain.ZsyhServiceTypeMoney;
import com.ruoyi.zsyh.domain.ZsyhServiceTypeMoneyother;
import com.ruoyi.zsyh.domain.ZsyhUserRepairstore;
import com.ruoyi.zsyh.service.IZsyhServiceTypeMoneyService;
import com.ruoyi.zsyh.service.IZsyhServiceTypeMoneyotherService;
import com.ruoyi.zsyh.service.IZsyhUserRepairstoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.*;

/**
 * 个人单
 */
@Controller
@RequestMapping("/applet/zsyh/serviceTypeMoney")
public class ZsyhServiceTypeMoneyAppletController extends BaseController
{


    @Autowired
    private IZsyhServiceTypeMoneyService zsyhServiceTypeMoneyService;

    @Autowired
    private IZsyhServiceTypeMoneyotherService zsyhServiceTypeMoneyotherService;


    @Autowired
    private IZsyhUserRepairstoreService zsyhUserRepairstoreService;

    /**
     * 新增收费标准
     * @return
     */
    @RequestMapping("/saveTypeMoney")
    @ResponseBody
    public AjaxResult saveTypeMoney(String userId, String rakerQuota, String rakerCommission, String typeMoneyJson)throws Exception
    {
        ZsyhUserRepairstore store = zsyhUserRepairstoreService.selectZsyhUserRepairstoreByUserId(userId);
        String storeId = store.getId();//师傅的店铺id

        //先删除原有的收费标准
        zsyhServiceTypeMoneyService.deleteZsyhServiceTypeMoneyByUserStoreId(storeId);

        List<Map<String, Object>> list = JSON.parseObject(typeMoneyJson, new TypeReference<List<Map<String, Object>>>() {});
//        List<ZsyhServiceTypeMoney> typeMoneyList = new ArrayList<>();
        for(Map<String, Object> a:list){
            String serviceTypeId = String.valueOf(a.get("serviceTypeId"));
            String serviceTypeName = String.valueOf(a.get("serviceTypeName"));
            String name = String.valueOf(a.get("name"));
            String money = String.valueOf(a.get("money"));
            ZsyhServiceTypeMoney typeMoney = new ZsyhServiceTypeMoney();
            typeMoney.setId(IdWorker.getId());
            typeMoney.setServiceTypeId(serviceTypeId);
            typeMoney.setServiceTypeName(serviceTypeName);
            typeMoney.setName(name);
            typeMoney.setMoney(money);
            typeMoney.setUserStoreId(storeId);
            zsyhServiceTypeMoneyService.insertZsyhServiceTypeMoney(typeMoney);
        }
        ZsyhUserRepairstore store1 = new ZsyhUserRepairstore();
        store1.setId(storeId);
        store1.setRakerQuota(rakerQuota);
        store1.setRakerCommission(rakerCommission);
        zsyhUserRepairstoreService.updateZsyhUserRepairstoreApplet(store1);

        return success("成功");
    }

    /**
     * 新增收费标准 -- 其他费用
     * @return
     */
    @Transactional
    @RequestMapping("/saveTypeMoneyOther")
    @ResponseBody
    public AjaxResult saveTypeMoneyOther(String userId, String rakerQuota, String rakerCommission, String typeMoneyJson)throws Exception
    {
        ZsyhUserRepairstore store = zsyhUserRepairstoreService.selectZsyhUserRepairstoreByUserId(userId);
        String storeId = store.getId();//师傅的店铺id

        //先删除原有的收费标准
        zsyhServiceTypeMoneyotherService.deleteZsyhServiceTypeMoneyotherByUserStoreId(storeId);

        List<Map<String, Object>> list = JSON.parseObject(typeMoneyJson, new TypeReference<List<Map<String, Object>>>() {});
//        List<ZsyhServiceTypeMoneyother> typeMoneyList = new ArrayList<>();
        for(Map<String, Object> a:list){
            String serviceTypeId = String.valueOf(a.get("serviceTypeId"));
            String serviceTypeName = String.valueOf(a.get("serviceTypeName"));
            String name = String.valueOf(a.get("name"));
            String money = String.valueOf(a.get("money"));
            ZsyhServiceTypeMoneyother typeMoneyother = new ZsyhServiceTypeMoneyother();
            typeMoneyother.setId(IdWorker.getId());
            typeMoneyother.setServiceTypeId(serviceTypeId);
            typeMoneyother.setServiceTypeName(serviceTypeName);
            typeMoneyother.setName(name);
            typeMoneyother.setMoney(money);
            typeMoneyother.setUserStoreId(storeId);
            zsyhServiceTypeMoneyotherService.insertZsyhServiceTypeMoneyother(typeMoneyother);
        }
        ZsyhUserRepairstore store1 = new ZsyhUserRepairstore();
        store1.setId(storeId);
        store1.setRakerQuota(rakerQuota);
        store1.setRakerCommission(rakerCommission);
        zsyhUserRepairstoreService.updateZsyhUserRepairstoreApplet(store1);

        return success("成功");
    }

    /**
     * 获取网点设置的收费标准
     * @return
     */
    @RequestMapping("/getTypeMoneyAllList")
    @ResponseBody
    public AjaxResult getTypeMoneyAllList(String userId)throws Exception
    {
        ZsyhUserRepairstore store = zsyhUserRepairstoreService.selectZsyhUserRepairstoreByUserId(userId);
        String storeId = store.getId();//师傅的店铺id


        ZsyhServiceTypeMoney money = new ZsyhServiceTypeMoney();
        money.setUserStoreId(storeId);
        List<ZsyhServiceTypeMoney> moneyList = zsyhServiceTypeMoneyService.selectZsyhServiceTypeMoneyList(money);
        List list = new ArrayList();
        Map<String, String> map = new HashMap<>();
        for(ZsyhServiceTypeMoney a:moneyList){
            Map<String, Object> map1 = new HashMap<>();
            String serviceTypeId = a.getServiceTypeId();
            if(EmptyUtils.isEmpty(map.get(serviceTypeId))){
                map.put(serviceTypeId, serviceTypeId);
                List list1 = new ArrayList();
                for(ZsyhServiceTypeMoney b:moneyList){
                    if(serviceTypeId.equals(b.getServiceTypeId())){
                        list1.add(b);
                    }
                }
                map1.put("name", a.getServiceTypeName());
                map1.put("id", a.getServiceTypeId());
                map1.put("list", list1);
                list.add(map1);
            }
        }

        return success("成功", list);
    }

    /**
     * 获取网点设置的收费标准 -- 其他费用
     * @return
     */
    @RequestMapping("/getTypeMoneyOtherAllList")
    @ResponseBody
    public AjaxResult getTypeMoneyOtherAllList(String userId)throws Exception
    {
        ZsyhUserRepairstore store = zsyhUserRepairstoreService.selectZsyhUserRepairstoreByUserId(userId);
        String storeId = store.getId();//师傅的店铺id


        ZsyhServiceTypeMoneyother money = new ZsyhServiceTypeMoneyother();
        money.setUserStoreId(storeId);
        List<ZsyhServiceTypeMoneyother> moneyList = zsyhServiceTypeMoneyotherService.selectZsyhServiceTypeMoneyotherList(money);
        List list = new ArrayList();
        Map<String, String> map = new HashMap<>();
        for(ZsyhServiceTypeMoneyother a:moneyList){
            Map<String, Object> map1 = new HashMap<>();
            String serviceTypeId = a.getServiceTypeId();
            if(EmptyUtils.isEmpty(map.get(serviceTypeId))){
                map.put(serviceTypeId, serviceTypeId);
                List list1 = new ArrayList();
                for(ZsyhServiceTypeMoneyother b:moneyList){
                    if(serviceTypeId.equals(b.getServiceTypeId())){
                        list1.add(b);
                    }
                }
                map1.put("name", a.getServiceTypeName());
                map1.put("id", a.getServiceTypeId());
                map1.put("list", list1);
                list.add(map1);
            }
        }

        return success("成功", list);
    }

}
